import AsyncStorage from '@react-native-community/async-storage';
import { CHAT_ROOM_LAST_MESSAGE_IDS } from '../constant';

let lastMessageIds: { [key: string]: number } = {};

AsyncStorage.getItem(CHAT_ROOM_LAST_MESSAGE_IDS).then((res: any): void => {
  if (res) {
    const ids = JSON.parse(res);
    lastMessageIds = ids;
  }
});

export const setLastMessageId = (
  id: number,
  lessonId: number,
  userId: number
): void => {
  lastMessageIds[`${lessonId}-${userId}`] = id;
  AsyncStorage.setItem(
    CHAT_ROOM_LAST_MESSAGE_IDS,
    JSON.stringify(lastMessageIds)
  );
};

export const getLastMessageIdWithLessonId = (
  lessonId: number,
  userId: number
): number => {
  return lastMessageIds[`${lessonId}-${userId}`];
};
